using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Box2D.XNA;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace WindowsPhoneGame1
{
    class Stage : GameComponent
    {
        protected World _world;
        protected List<BaseObject> actors = new List<BaseObject>();
        Camera2D camera;
        SpriteFont Font;
        Animation a;

        public Stage(Game game)
            : base(game)
        {
            Initialize();
        }

        public override void Initialize()
        {
            Vector2 gravity;
            gravity = new Vector2(0.0f, -10.0f);
            bool doSleep = true;
            _world = new World(gravity, doSleep);

            GroundObject grount = new GroundObject(this._world);
            grount.SetPostion(new Vector2(400, 10));
            grount.SetSize(new Vector2(800, 20));
            Actors.Add(grount);

            PhysicObject p = new PhysicObject(this._world);
            Actors.Add(p);

            

            Texture2D tex = ResourceManager.LoadTexture("images/bg");
            a = new Animation(tex, new Point(1440, 900));
            camera = new Camera2D(this.Game);
            camera.Focus = p;
            //camera.Position
            Font = ResourceManager.LoadSpriteFont("SpriteFont1");
            //Font.
        }

        public List<BaseObject> Actors
        {
            get
            {
                return actors;
            }
        }

        public virtual void OnDraw(SpriteBatch spriteBatch)
        {
            spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, null, null, null, null,
                                camera.Transform);
            a.OnDraw(spriteBatch, Vector2.Zero);
            foreach (BaseObject b in actors)
                b.OnDraw(spriteBatch);





            //spriteBatch.DrawString(Font, "X"+camera.Position.X+",Y"+camera.Position.Y,new Vector2(250,50),Color.Sienna,0,new Vector2(0,0),2,SpriteEffects.None,1);
            spriteBatch.DrawString(Font, "X" + Game.GraphicsDevice.Viewport.Width + 
                ",Y" + Game.GraphicsDevice.Viewport.Height, new Vector2(250, 50), Color.Sienna, 0, new Vector2(0, 0), 2, SpriteEffects.None, 1);

            spriteBatch.End();
        }

        public virtual void Update(int delta)
        {
            camera.Update(delta);
            a.Update(delta);
            _world.Step(1f / 60, 10, 10);
            foreach (BaseObject b in actors)
            {
                b.Update(delta);
            }
        }
    }
}
